﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AmuTools;
using app.Api;
using System.Text.RegularExpressions;

namespace app.Models
{
    [Model(TableName = "t_member", PrimaryKey = "id", IdentityInsert = false, BaseID = "200000")]
    public class Member : ModelBase
    {
        [Field(Description = "唯一标识符")]
        public int id { get; set; }


        [Field(DataType = "nvarchar(MAX)", Description = "编号 = c/a/p + id")]
        public string number { get; set; }


        [Field(Description = "用户名")]
        public string username { get; set; }


        [Field(Webable = false, DataType = "varchar(50)", Description = "密码")]
        public string password { get; set; }


        [Field(Description = "联系人姓名")]
        public string name { get; set; }


        [Field(Description = "联系人性别：0女，1男")]
        public int sex { get; set; }


        [Field(FieldName = "mobile_phone", Webable = false, DataType = "varchar(11)", Description = "联系人隐藏联系方式，其实这个字段原来是个实验品，现在继承下来了也没关系")]
        public string h_mobile_phone { get; set; }


        [Field(Storageable = false, Description = "联系人对外显示联系方式")]
        public string mobile_phone
        {
            get
            {
                return h_mobile_phone;
            }
        }


        [Field(DataType = "char(13)", Description = "注册日期")]
        public string create_time { get; set; }


        [Field(Description = "部门")]
        public string department { get; set; }


        [Field(Description = "统一社会信用代码")]
        public string usci { get; set; }


        [Field(FieldName = "email", Webable = false, Description = "联系人隐藏邮箱")]
        public string h_email { get; set; }


        [Field(Storageable = false, Description = "联系人对外显示邮箱")]
        public string email {
            get
            {
                string temp = h_email;
                Regex re = new Regex(@"\w{3}(?=@\w+?.\S+)", RegexOptions.None);
                return re.Replace(temp, "****");
            }
        }


        [Field(Description = "企业名称")]
        public string enterprise_name { get; set; }


        [Field(Description = "企业/机构类别，在字典中选择")]
        public string member_type { get; set; }


        [Field(DataType = "varchar(MAX)", Description = "企业简介")]
        public string enterprise_profile { get; set; }


        [Field(Description = "注册资本")]
        public int registered_capital { get; set; }


        [Field(Description = "营业执照编码")]
        public string business_licence { get; set; }


        [Field(DataType = "text", Description = "营业执照照片")]
        public string business_licence_pic { get; set; }


        [Field(Description = "所处地区")]
        public string area { get; set; }


        [Field(Description = "详细地址")]
        public string address { get; set; }


        [Field(DataType = "text", Description = "logo")]
        public string logo { get; set; }


        [Field(DataType = "text", Description = "详细地址坐标")]
        public string coordinate { get; set; }


        [Field(Description = "手机是否认证")]
        public bool is_mobile_phone_authed { get; set; }


        [Field(Description = "邮箱是否已验证")]
        public bool is_email_authed { get; set; }


        //[Field(Description = "企业机构认证状态：0未操作，1待审核，2已审核，3审核失败")]
        //public int enterprise_authed_status { get; set; }


        [Field(Description = "会员身份类型：3个人，4企业，5服务机构")]
        public int role_type { get; set; }


        [Field(DataType = "varchar(100)", Description = "角色，允许一个会员有多个角色，直观理解比如一个qq会员可以是群成员和群主两个角色。目前系统并没有用到这个字段")]
        public string role_ids { get; set; }


        [Field(Webable = false, DataType = "text", Description = "用户自己的特殊权限，其中可以包含被禁用的权限")]
        public string special_permission { get; set; }


        [Field(Description = "身份证号")]
        public string identity_card { get; set; }


        [Field(Description = "身份证是否认证")]
        public bool is_identity_card_authed { get; set; }


        [Field(Description = "身份证正面图片")]
        public string identity_card_front { get; set; }


        [Field(Description = "身份证反面图片")]
        public string identity_card_verso { get; set; }


        private string _permission = null;
        [Field(Storageable = false, Description = "最后的拼接权限，根据角色权限和账户特殊权限来做的")]
        public string permission
        {
            get
            {
                if (_permission == null)
                {
                    Member member = G.DB.GetById<Member>(id);
                    SqlResult result = G.DB.Get(string.Format("select * from {0} where '{1}' like '%'+convert(varchar,id)+'%'", G.DB.GetTableName<AdminRole>(), member.role_ids));
                    List<AdminRole> roles = result.GetFirstTableList<AdminRole>();
                    string permission = "";
                    foreach (AdminRole item in roles)
                    {
                        permission += item.permission;
                    }
                    _permission = permission + this.special_permission;
                }
                return _permission;
            }
        }


        [Field(Description = "会员状态：0未审核，1正在审核，3审核失败，2已通过")]
        public int status { get; set; }


        [Field(Description = "帐号是否被封禁")]
        public int is_ban { get; set; }

        
        [Field(Description = "用户评分")]
        public double rate { get; set; }


        [Field(Webable = false, Description = "是否已被删除")]
        public int is_delete { get; set; }


        [Field(Storageable = false, Description = "登陆状态：0离线，1在线，2离开")]
        public int landing_state
        {
            get
            {
                return (int)Api.LandingServer.GetUnitState(this.id.ToString());
            }
        }

        
        [Field(Storageable = false, Description = "未读消息数量")]
        public int unread_message_count
        {
            get
            {
                Cd cd = new Cd();
                cd.And("m_type=0");
                cd.And("status=0");
                cd.And("user_id='{0}'", api_base.UserCode);
                return G.DB.GetCount<MemberMessage>(cd.Render());
            }
        }

        
        [Field(Storageable = false, Description = "已读消息数量")]
        public int read_message_count
        {
            get
            {
                Cd cd = new Cd();
                cd.And("m_type=0");
                cd.And("status=1");
                cd.And("user_id='{0}'", api_base.UserCode);
                return G.DB.GetCount<MemberMessage>(cd.Render());
            }
        }

        
        [Field(Storageable = false, Description = "回收消息数量")]
        public int recycle_message_count
        {
            get
            {
                Cd cd = new Cd();
                cd.And("m_type=0");
                cd.And("status=2");
                cd.And("user_id='{0}'", api_base.UserCode);
                return G.DB.GetCount<MemberMessage>(cd.Render());
            }
        }

        
        [Field(Storageable = false, Description = "发起的约谈数量")]
        public int s_provider_count
        {
            get
            {
                Cd cd = new Cd();
                cd.And("service_status=2");
                cd.And("demand_status=0");
                cd.And("service_member_id='{0}'", api_base.UserCode);
                return G.DB.GetCount<Abutment>(cd.Render());
            }
        }

        
        [Field(Storageable = false, Description = "收到的投递数量")]
        public int r_demander_count
        {
            get
            {
                Cd cd = new Cd();
                cd.And("demand_status=2");
                cd.And("service_status=0");
                cd.And("service_member_id='{0}'", api_base.UserCode);
                return G.DB.GetCount<Abutment>(cd.Render());
            }
        }

        
        [Field(Storageable = false, Description = "发起的投递数量")]
        public int s_demander_count
        {
            get
            {
                Cd cd = new Cd();
                cd.And("service_status=2");
                cd.And("demand_status=0");
                cd.And("demand_member_id='{0}'", api_base.UserCode);
                return G.DB.GetCount<Abutment>(cd.Render());
            }
        }

        
        [Field(Storageable = false, Description = "收到的约谈数量")]
        public int r_provider_count
        {
            get
            {
                Cd cd = new Cd();
                cd.And("service_status=0");
                cd.And("demand_status=2");
                cd.And("demand_member_id='{0}'", api_base.UserCode);
                return G.DB.GetCount<Abutment>(cd.Render());
            }
        }

        
        [Field(Storageable = false, Description = "正在约谈数量")]
        public int chatting_count
        {

            get
            {
                if (api_base.UserRole == (int)ROLE_TYPES.SERVICE)
                {
                    Cd cd = new Cd();
                    cd.And("demand_status=2");
                    cd.And("service_status=2");
                    cd.And("service_member_id='{0}'", api_base.UserCode);
                    return G.DB.GetCount<Abutment>(cd.Render());
                }
                else
                {
                    Cd cd = new Cd();
                    cd.And("demand_status=2");
                    cd.And("service_status=2");
                    cd.And("demand_member_id='{0}'", api_base.UserCode);
                    return G.DB.GetCount<Abutment>(cd.Render());
                }

            }
        }

        
        [Field(Storageable = false, Description = "正在合作数量")]
        public int comming_count
        {
            get
            {
                if (api_base.UserRole == (int)ROLE_TYPES.SERVICE)
                {
                    Cd cd = new Cd();
                    cd.And("demand_status=2");
                    cd.And("service_status=2");
                    cd.And("service_member_id='{0}'", api_base.UserCode);
                    return G.DB.GetCount<Abutment>(cd.Render());
                }
                else
                {
                    Cd cd = new Cd();
                    cd.And("demand_status=2");
                    cd.And("service_status=2");
                    cd.And("demand_member_id='{0}'", api_base.UserCode);
                    return G.DB.GetCount<Abutment>(cd.Render());
                }
            }
        }

        
        [Field(Description = "合作成功数量")]
        public int success_count { get; set; }

        
        [Field(Storageable = false, Description = "已发布服务数量")]
        public int posted_count
        {

            get
            {
                if (api_base.UserRole == (int)ROLE_TYPES.SERVICE)
                {
                    Cd cd = new Cd();
                    cd.And("member_id='{0}'", api_base.UserCode);
                    return G.DB.GetCount<ServiceItem>(cd.Render());
                }
                else
                {
                    Cd cd = new Cd();
                    cd.And("member_id='{0}'", api_base.UserCode);
                    return G.DB.GetCount<DemandItem>(cd.Render());
                }
            }
        }

        
        [Field(Storageable = false, Description = "上次登录日志")]
        public Log last_login_log
        {
            get
            {
                Cd cd = new Cd();
                cd.And("user_id='{0}'", api_base.UserCode);
                cd.And("operation='{0}'", (int)PERMISSIONOPERATION.LOGIN);
                Ob ob = new Ob();
                ob.Desc("create_time");
                List<Log> list = G.DB.Get<Log>(cd.Render(), ob.Render(), 2).GetFirstTableList();
                return list.Count == 2 ? list[1] : null;
            }
        }

        
        [Field(Storageable = false, IsPrivate = true, Description = "已申请的平台服务列表")]
        public List<PlateformServiceAbutment>successplateform
        {
            get
            {
                Cd cd = new Cd();
                cd.And("company_id='{0}'", id);
                cd.And("status=2");
                Ob ob = new Ob();
                ob.Desc("id");
                List<PlateformServiceAbutment> list = G.DB.Get<PlateformServiceAbutment>(cd.Render(), ob.Render()).GetFirstTableList();
                return list;
            }
        }


        static public string GetSearchSql(int id, int role_type, string username, string area, string member_type, string title, string begin_time, string end_time, int status)
        {
            Cd cd = new Cd();
            if (id != 0) cd.And("id = {0} ", id);
            if (role_type != 0) cd.And("role_type = {0} ", role_type);
            if (!"".Equals(username)) cd.And("username like '%{0}%'", username);
            if (!"".Equals(area)||!",,".Equals(area)) cd.And("area like '{0}%'", area);
            if ((!"".Equals(member_type)) && (!"*".Equals(member_type))) cd.And("member_type = '{0}'", member_type);
            if (title != "") cd.And("enterprise_name like '%{0}%'", title);
            if (begin_time != "") cd.And("create_time >= {0}", begin_time);
            if (end_time != "") cd.And("create_time < {0}", end_time);
            cd.And("is_delete <> 1");
            if (status != -1) cd.And("status = {0}", status);
            return cd.Render();
        }


        static public Dictionary<string, string> ExcelColumns
        {
            get
            {
                Dictionary<string, string> dic = new Dictionary<string, string>();
                dic.Add("number", "编号");
                dic.Add("username", "用户名");
                dic.Add("name", "名字");
                return dic;
            }
        }
    }


    enum ROLE_TYPES
    {
        ANY = 0, // 任意
        GUEST = 1, // 游客
        ADMIN = 2, // 管理员
        PERSONAL = 3, // 个人
        ENTERPRISE = 4, // 企业
        SERVICE = 5, // 服务机构
        GOVERNMENT = 6 // 监管部门
    }

    class SPONSOR_TYPES
    {
        public static string Demander = "demander"; // 需求者
        public static string Provider = "provider"; // 服务者
    }

    enum AUTH_STATUS
    {
        NONE = 0, // 未审核
        EXAMINING = 1, // 正在审核
        PASS = 2, // 已通过
        FAILED_EXAMINE = 3, // 审核失败
    }
}